package marco.android.CouchControl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import marco.android.CouchControl.R;

public class Serializer
{
	public static Object toObject(byte [] b)
	{
		Object o = null;
		ByteArrayInputStream bais = new ByteArrayInputStream(b);
		try
		{
			ObjectInputStream ois = new ObjectInputStream(bais);
			o = ois.readObject();
			ois.close();
		}
		catch(IOException e)
		{
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return o;
	}
	
	public static byte [] toByteArray(Object o)
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte [] b = null;
		try
		{
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(o);
			b = baos.toByteArray();
			oos.close();
		}
		catch(IOException e)
		{
			e.getStackTrace();
			e.getMessage();
		}
		return b;
	}
//	public class BitsToFloat
//	{
//	public static void main(String[] args)
//	{
//	byte[] b = new byte[] { 0x01, 0x02, 0x03, 0x04 };
//
//	System.out.println("toFloat: " + toFloat(b));
//	System.out.println("convertByteArrayToFloat: "
//	+ convertByteArrayToFloat(b));
//	}
//
//	// Quick, low-overhead conversion taking advantage of knowledge of
//	// how Java effects these conversions
//	public static float toFloat(byte[] b)
//	{
//	if (b.length != 4) return 0.0F;
//
//	// Same as DataInputStream's 'readInt' method
//	int i = (((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16) |
//	((b[2] & 0xff) << | (b[3] & 0xff));
//
//	// This converts bits as follows:
//	/*
//	int s = ((i >> 31) == 0) ? 1 : -1;
//	int e = ((i >> 23) & 0xff);
//	int m = (e == 0) ? (i & 0x7fffff) << 1 : (i & 0x7fffff) | 0x800000;
//	*/
//
//	return Float.intBitsToFloat(i);
//	}
}
